//
//  BaseViewController.m
//  testnav
//
//  Created by guolihuiguolihui on 15/5/12.
//  Copyright (c) 2015年 guolihuiguolihui. All rights reserved.
//

#import "UIViewController+GhCustomNavigation.h"
#import <objc/runtime.h>

@implementation UIViewController (GhCustomNavigation)
@dynamic thbNavigationItem;
@dynamic thbNavigationBar;
-(UINavigationBar*)createThbNavigationBar {
    CGRect navFrame = self.navigationController.navigationBar.frame;
    
    UINavigationBar* navgationBar = [[UINavigationBar alloc] init];
    navgationBar.frame = navFrame;
    UIImage* bgImage = [UIImage imageNamed:@"bg.jpg"];
    
    UIImageView* statusBgView = [[UIImageView alloc] initWithImage:bgImage];
    CGRect frame = [UIApplication sharedApplication].statusBarFrame;
    frame.origin.y -= frame.size.height;
    frame.size.height += navFrame.size.height;
    statusBgView.frame = frame;
    
    [navgationBar addSubview:statusBgView];
    return navgationBar;
}
-(UINavigationBar*)addThbNavigationBar {
    UINavigationBar* bar = self.thbNavigationBar;
    [self.view addSubview:bar];
    [self.view sendSubviewToBack:bar];
    return bar;
}
-(UINavigationItem*)addThbNavigtionItem {
    UINavigationBar* navgationBar = [self addThbNavigationBar];
    UINavigationItem* item = self.thbNavigationItem;
    [navgationBar pushNavigationItem:item animated:NO];
    return item;
}
-(UINavigationItem*)thbNavigationItem {
    UINavigationItem* navigationItem = objc_getAssociatedObject(self, _cmd);
    if (navigationItem) return navigationItem;
    navigationItem = [[UINavigationItem alloc] init];
    objc_setAssociatedObject(self, _cmd, navigationItem, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    return navigationItem;
}
-(UINavigationBar*)thbNavigationBar {
    UINavigationBar* bar = objc_getAssociatedObject(self, _cmd);
    if (bar) return bar;
    bar = [self createThbNavigationBar];
    objc_setAssociatedObject(self, _cmd, bar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    return bar;
}
@end
